package com.nightsoul.shiro.test.chapter5.hash.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.PasswordService;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class MyRealm extends AuthorizingRealm {
	private PasswordService passwordService;

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
		authorizationInfo.addRole("role1");
		return authorizationInfo;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// 所有用户都通过验证
		String username = token.getPrincipal().toString();
		String password = new String((char[])token.getCredentials());
		return new SimpleAuthenticationInfo(username, 
				this.passwordService.encryptPassword(password), getName());
	}
	
	public PasswordService getPasswordService() {
		return passwordService;
	}
	public void setPasswordService(PasswordService passwordService) {
		this.passwordService = passwordService;
	}
}
